package com.open.system.controller;

import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
import com.open.system.domain.*;
import com.open.system.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 */
 
@Controller
@RequestMapping("/system/loanUser")
public class LoanUserController extends BaseController {
	@Autowired
	private LoanUserService loanUserService;
	
	@Autowired
	private LoanContactService loanContactService;
	
	@Autowired
	private LoanBankService loanbankservice;
	
	@Autowired
	private LoanFaceService loanFaceService;
	
	@Autowired
	private LoanApplyService loanApplyService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;

	@Autowired
	private LoanJuxinliPostInfoService loanJuxinliPostInfoService;

	@GetMapping()
	@RequiresPermissions("system:loanUser:loanUser")
	String LoanUser(){
	    return "system/loanUser/loanUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanUser:loanUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="注册用户";
		String detail="查看了注册用户列表";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		//查询列表数据
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("remark2",shopId);
		}
        Query query = new Query(params);
		List<LoanUserDO> loanUserList = loanUserService.list(query);
		for (int i = 0; i < loanUserList.size(); i++) {
			LoanUserDO use=loanUserList.get(i);
			use.setHasShowSysName(shopId);
		}
		int total = loanUserService.count(query);
		PageUtils pageUtils = new PageUtils(loanUserList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanUser:add")
	String add(){
	    return "system/loanUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanUser:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanUserDO loanUser = loanUserService.get(id);
		model.addAttribute("loanUser", loanUser);
	    return "system/loanUser/edit";
	}
	
	@GetMapping("/details/{id}")
	@RequiresPermissions("system:loanUser:details")
	String details(@PathVariable("id") Long id,Model model){
		LoanUserDO loanUser = loanUserService.get(id);
		
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="申请认证";
		String detail="点击头像查看了【"+loanUser.getRealName()+"】的认证信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("userId", loanUser.getId());
		List<LoanContactDO>contactDos=loanContactService.list(map);
		if(contactDos!=null && contactDos.size()>0){
			LoanContactDO contact=contactDos.get(0);
			loanUser.setcName1(contact.getcName1());
			loanUser.setcName2(contact.getcName2());
			loanUser.setcName3(contact.getcName3());
			loanUser.setcMoblie1(contact.getcMoblie1());
			loanUser.setcMoblie2(contact.getcMoblie2());
			loanUser.setcMoblie3(contact.getcMoblie3());
			loanUser.setcRelationship1(contact.getcRelationship1());
			loanUser.setcRelationship2(contact.getcRelationship2());
			loanUser.setcRelationship3(contact.getcRelationship3());
		}
		
		//银行卡
		List<LoanBankDO>loanBankDOs=loanbankservice.list(map);
		if(loanBankDOs!=null && loanBankDOs.size()>0){
			LoanBankDO loanBankDO=loanBankDOs.get(0);
			loanUser.setBankName(loanBankDO.getBankName());
			loanUser.setIdNo(loanBankDO.getIdNo());
			loanUser.setCardNo(loanBankDO.getCardNo());
			if("2".equals(loanBankDO.getStatus())){
				loanUser.setBankStatus("已认证");
			}else {
				loanUser.setBankStatus("未认证");
			}
			
			}else {
			loanUser.setBankStatus("未认证");
		}
		
		//人脸认证
		List<LoanFaceDO>faceDOs=loanFaceService.list(map);
		if(faceDOs!=null && faceDOs.size()>0){
			LoanFaceDO faceDO=faceDOs.get(0);
			loanUser.setScore(faceDO.getScore());
			loanUser.setCreate(faceDO.getCreate());
			loanUser.setNumStatus(faceDO.getStatus());
			loanUser.setIdentify(faceDO.getIdentify());
			if("1".equals(faceDO.getFaceId1()) && "2".equals(faceDO.getFaceId2())){
				loanUser.setPic1(faceDO.getPic1());
				loanUser.setPic2(faceDO.getPic2());
				loanUser.setPic3(faceDO.getPic3());
				if("1".equals(faceDO.getStatus())){
					loanUser.setFaceStatus("审核中");
				}else if("2".equals(faceDO.getStatus())){
					loanUser.setFaceStatus("已认证");
				}else if(StringUtils.isNotEmpty(faceDO.getPic1()) && StringUtils.isNotEmpty(faceDO.getPic2())){
					loanUser.setFaceStatus("已驳回");
					loanUser.setPic1(faceDO.getPic1());
					loanUser.setPic2(faceDO.getPic2());
					loanUser.setPic3(faceDO.getPic3());
				}else {
					loanUser.setFaceStatus("未认证");
					loanUser.setPic1("/img/pic_head.jpg");
					loanUser.setPic2("/img/pic_head.jpg");
					loanUser.setPic3("/img/pic_head.jpg");
				}
			}else {
				if("2".equals(faceDO.getStatus())){
					loanUser.setPic1("data:image/png;base64,"+faceDO.getPic1());
					loanUser.setPic2("data:image/png;base64,"+faceDO.getPic2());
					loanUser.setPic3("data:image/png;base64,"+faceDO.getPic3());
					loanUser.setFaceStatus("已认证");
				}else {
					loanUser.setFaceStatus("未认证");
					loanUser.setPic1("/img/pic_head.jpg");
					loanUser.setPic2("/img/pic_head.jpg");
					loanUser.setPic3("/img/pic_head.jpg");
				}
			}
		}else {
			loanUser.setFaceStatus("未认证");
			loanUser.setPic1("/img/pic_head.jpg");
			loanUser.setPic2("/img/pic_head.jpg");
			loanUser.setPic3("/img/pic_head.jpg");
		}
		model.addAttribute("loanUser", loanUser);
	    return "system/loanUser/details";
	}
	@GetMapping("/downLoadDetails/{id}")
	@RequiresPermissions("system:loanUser:details")
	String downLoadDetails(@PathVariable("id") Long id,Model model){
		LoanUserDO loanUser = loanUserService.get(id);

		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="申请认证";
		String detail="点击头像查看了【"+loanUser.getRealName()+"】的认证信息";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--

		Map<String, Object> map=new HashMap<String, Object>();
		map.put("userId", loanUser.getId());
        map.put("hasempty", "1");
		List<LoanContactDO>contactDos=loanContactService.list(map);
		if(contactDos!=null && contactDos.size()>0){
			LoanContactDO contact=contactDos.get(0);
			loanUser.setcName1(contact.getcName1());
			loanUser.setcName2(contact.getcName2());
			loanUser.setcName3(contact.getcName3());
			loanUser.setcMoblie1(contact.getcMoblie1());
			loanUser.setcMoblie2(contact.getcMoblie2());
			loanUser.setcMoblie3(contact.getcMoblie3());
			loanUser.setcRelationship1(contact.getcRelationship1());
			loanUser.setcRelationship2(contact.getcRelationship2());
			loanUser.setcRelationship3(contact.getcRelationship3());
		}

		//银行卡
		List<LoanBankDO>loanBankDOs=loanbankservice.list(map);
		if(loanBankDOs!=null && loanBankDOs.size()>0){
			LoanBankDO loanBankDO=loanBankDOs.get(0);
			loanUser.setBankName(loanBankDO.getBankName());
			loanUser.setIdNo(loanBankDO.getIdNo());
			loanUser.setCardNo(loanBankDO.getCardNo());
			if("2".equals(loanBankDO.getStatus())){
				loanUser.setBankStatus("已认证");
			}else {
				loanUser.setBankStatus("未认证");
			}

			}else {
			loanUser.setBankStatus("未认证");
		}

		//人脸认证
        List<LoanFaceDO> faceDOs = loanFaceService.list1(map);
		if(faceDOs!=null && faceDOs.size()>0){
			LoanFaceDO faceDO=faceDOs.get(0);
			loanUser.setScore(faceDO.getScore());
			loanUser.setCreate(faceDO.getCreate());
			loanUser.setNumStatus(faceDO.getStatus());
			loanUser.setIdentify(faceDO.getIdentify());
			if("1".equals(faceDO.getFaceId1()) && "2".equals(faceDO.getFaceId2())){
				loanUser.setPic1(faceDO.getPic1());
				loanUser.setPic2(faceDO.getPic2());
				loanUser.setPic3(faceDO.getPic3());
				if("1".equals(faceDO.getStatus())){
					loanUser.setFaceStatus("审核中");
				}else if("2".equals(faceDO.getStatus())){
					loanUser.setFaceStatus("已认证");
				}else if(StringUtils.isNotEmpty(faceDO.getPic1()) && StringUtils.isNotEmpty(faceDO.getPic2())){
					loanUser.setFaceStatus("已驳回");
					loanUser.setPic1(faceDO.getPic1());
					loanUser.setPic2(faceDO.getPic2());
					loanUser.setPic3(faceDO.getPic3());
				}else {
					loanUser.setFaceStatus("未认证");
					loanUser.setPic1("/img/pic_head.jpg");
					loanUser.setPic2("/img/pic_head.jpg");
					loanUser.setPic3("/img/pic_head.jpg");
				}
			}else {
				if("2".equals(faceDO.getStatus())){
					loanUser.setPic1("data:image/png;base64,"+faceDO.getPic1());
					loanUser.setPic2("data:image/png;base64,"+faceDO.getPic2());
					loanUser.setPic3("data:image/png;base64,"+faceDO.getPic3());
					loanUser.setFaceStatus("已认证");
				}else {
					loanUser.setFaceStatus("未认证");
					loanUser.setPic1("/img/pic_head.jpg");
					loanUser.setPic2("/img/pic_head.jpg");
					loanUser.setPic3("/img/pic_head.jpg");
				}
			}
		}else {
			loanUser.setFaceStatus("未认证");
			loanUser.setPic1("/img/pic_head.jpg");
			loanUser.setPic2("/img/pic_head.jpg");
			loanUser.setPic3("/img/pic_head.jpg");
		}
		model.addAttribute("loanUser", loanUser);
	    return "system/loanUser/detailsDownLoad";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanUser:add")
	public R save( LoanUserDO loanUser){
		loanUser.setCreateTime(new Date());
		if(loanUserService.save(loanUser)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanUser:edit")
	public R update( LoanUserDO loanUser){
		loanUserService.update(loanUser);
		return R.ok();
	}
	
	
	@ResponseBody
	@RequestMapping("/hasPass")
	@RequiresPermissions("system:loanUser:edit")
	public R pass(String id,String status){
		
		LoanUserDO userDO=loanUserService.get(Long.parseLong(id));
		if(userDO==null){
			return R.error(201,"参数对象不存在");
		}
		Map<String,Object>map=new HashMap<String,Object>();
		map.put("userId", userDO.getId());
		List<LoanFaceDO>faceList=loanFaceService.list(map);
		List<LoanApplyDO>applyList=loanApplyService.list(map);
		if(faceList!=null && faceList.size()>0 && applyList!=null &&applyList.size()>0){
			LoanFaceDO faceDO=faceList.get(0);
			LoanApplyDO applyDO=applyList.get(0);
			faceDO.setStatus(status);
			applyDO.setFaceAuth(status);
			loanFaceService.update(faceDO);
			loanApplyService.update(applyDO);
		}else {
			return R.error(202,"对象数据不存在");
		}
		//更改face表状态，apply的faceAuth表
		//0---0
		//2---2
		//只是认证而已，不是借款或者放款
		
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="申请认证";
		String detail="";
		if("0".equals(status)){
			detail="驳回了【"+userDO.getRealName()+"】身份证件认证";
		}else if("2".equals(status)){
			detail="通过了【"+userDO.getRealName()+"】身份证件认证";
		}
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanUser:remove")
	public R remove( Long id){
		if(loanUserService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanUser:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanUserService.batchRemove(ids);
		return R.ok();
	}
	
}
